﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IMessageChatRepository))]
    public class MessageChatRepository : IMessageChatRepository
    {
        private Connection conn;

        public MessageChatRepository()
        {
            conn = new Connection();
        }

        public void SaveMessageChat(MessageChat messageChat)
        {
            using (MXHEntities context = conn.GetContext())
            {
                context.MessageChats.AddObject(messageChat);
                context.SaveChanges();
            }
        }

        public List<MessageChat> GetMessageChatByAccountID(int currentID, int chatToID)
        {
            List<MessageChat> lstMessageChat = new List<MessageChat>();

            string strSQL = @"WITH MES AS (
                                SELECT TOP 20 *
                                FROM MessageChat
	                            WHERE (AccountIDSent = "+ currentID +" and AccountIDReceive = "+ chatToID +") or" + @"
		                              (AccountIDSent = "+ chatToID +" and AccountIDReceive = "+ currentID +")" + @"
                                ORDER BY Time DESC
                            )
                            SELECT * 
                            FROM MES
                            ORDER BY Time";
            using (MXHEntities context = conn.GetContext())
            {
                var lst = context.ExecuteStoreQuery<MessageChat>(strSQL);
                if (lst != null)
                    lstMessageChat = lst.ToList();
            }
            return lstMessageChat;
        }

        public int CountMessageChatUnReadByAccountID(int accountID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                var message = from mes in context.MessageChats
                              where mes.AccountIDReceive == accountID && mes.isRead == false
                              select mes;
                return message.Count();
            }
        }

        public List<MessageChat> GetMessageChatByAccountID(int accountID)
        {
            List<MessageChat> lstMessageChat = new List<MessageChat>();
            using (MXHEntities context = conn.GetContext())
            {
                var lst = from mes in context.MessageChats
                          where mes.AccountIDReceive == accountID && mes.isRead == false
                          select mes;
                lstMessageChat = lst.Count() > 0 ? lst.ToList() : null;
            }
            return lstMessageChat;
        }

        public void UpdateMessageChatIsRead(int MessageChatID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                MessageChat message = (from mes in context.MessageChats
                                      where mes.MessageChatID == MessageChatID
                                      select mes).FirstOrDefault();
                if (message != null)
                {
                    message.isRead = true;
                    context.SaveChanges();
                }
            }
        }

        public void DeleteMessageChatByAccountID(int accountID, int friendID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from mes in context.MessageChats
                           where (mes.AccountIDSent == accountID && mes.AccountIDReceive == friendID) ||
                                 (mes.AccountIDReceive == accountID && mes.AccountIDSent == friendID)
                           select mes;
                if (item.Count() > 0)
                {
                    List<MessageChat> lstMesChat = item.ToList();
                    foreach (MessageChat msg in lstMesChat)
                    {
                        context.MessageChats.DeleteObject(msg);
                        context.SaveChanges();
                    }
                }
            }
        }
    }
}
